package net.zjitc.controller;

import net.zjitc.common.ServiceResponse;
import net.zjitc.pojo.Notice;
import net.zjitc.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/1 9:11
 */
@Controller
@RequestMapping("/manage/notice")
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    @RequestMapping("findAll")
    public String findAll(Model model){
        List<Notice> list = noticeService.findAll();
        model.addAttribute("list",list);
        return "/admin/notices/list.jsp";
    }

    @RequestMapping("findById")
    public String findById(int id,Model model){
        Notice notice = noticeService.findById(id);
        model.addAttribute("notice",notice);
        return "/admin/notices/edit.jsp";
    }

    @RequestMapping("/editNotice")
    @ResponseBody
    public ServiceResponse editNotice(@RequestBody Notice notice, Model model){
        return noticeService.updateById(notice);
    }

    @RequestMapping("/addNotice")
    public String addNotice(Notice notice, Model model){
        Date date = new Date();
        notice.setN_time(date);
        int resultCount = noticeService.addNotice(notice);
        return "redirect:/manage/notice/findAll.do";
    }

    @RequestMapping("/deleteNotice")
    public String deleteNotice(int id,Model model){
        noticeService.deleteNotice(id);
        return "redirect:/manage/notice/findAll.do";
    }

}
